﻿using Microsoft.Win32;
using System;
using System.Globalization;
using System.Windows;
using System.Windows.Data;
using System.Windows.Input;

namespace 节拍计算器2
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public static 节拍计算器 计算器 = new 节拍计算器();
        public static MainWindow 运行时;
        public MainWindow()
        {
            运行时 = this;
            DataContext = 计算器;
            InitializeComponent();
        }

        private void 点按(object sender, MouseButtonEventArgs e)
        {
            进入节拍计算器主界面();
        }

        private void 进入节拍计算器主界面()
        {
            if (开始界面.Visibility == Visibility.Visible) { 开始界面.Visibility = Visibility.Collapsed; 日志.输出("已关闭开始界面"); }
            if (节拍界面.Visibility == Visibility.Collapsed) { 节拍界面.Visibility = Visibility.Visible; 日志.输出("已开启节拍界面"); }
            计算器.应用程序主状态 = 文本.进入节拍计算器后提示;
            计算器.应用程序副状态 = 文本.空文本;
        }

        private void BPM鼠标滚动(object sender, MouseWheelEventArgs e)
        {
            var 滚动值 = e.Delta;
            if (滚动值 > 0) { 计算器.BPM微调(节拍计算器.微调类型.向上); }
            if (滚动值 < 0) { 计算器.BPM微调(节拍计算器.微调类型.向下); }
        }

        private void 拍数修改(object sender, Panuon.WPF.SelectedValueChangedRoutedEventArgs<double?> e)
        {
            计算器.更新节拍点();
        }

        private void 界面重置(object sender, RoutedEventArgs e)
        {
            if (开始界面.Visibility == Visibility.Collapsed) { 开始界面.Visibility = Visibility.Visible; 日志.输出("已启动开始界面"); }
            if (节拍界面.Visibility == Visibility.Visible) { 节拍界面.Visibility = Visibility.Collapsed; 日志.输出("已关闭节拍界面"); }
            计算器.重置();
            日志.输出("已重置");
        }

        private void 计算节拍(object sender, RoutedEventArgs e)
        {
            计算器.追加新的间隔时间点();
        }

        private void 启动自动寻拍(object sender, RoutedEventArgs e)
        {
            if (计算器.推荐BPM == 0) { return; }
            if (!计算器.自动处理)
            {
                计算器.开始自动处理();

                return;

            }
            if (计算器.自动处理)
            {

                计算器.停止自动处理();
                return;
            }
        }

        private void 打开音频文件(object sender, RoutedEventArgs e)
        {

            if (打开音频文件()) { 进入节拍计算器主界面(); 计算器.播放控制选项状态 = Visibility.Visible; 打开音频参考.Visibility = Visibility.Collapsed; }
        }

        private static bool 打开音频文件()
        {
            OpenFileDialog 打开文件窗口 = new OpenFileDialog();
            打开文件窗口.Filter = "音频文件 (*.wav, *.mp3, *.ogg)|*.wav;*.mp3;*.ogg";
            打开文件窗口.Multiselect = false;

            if (打开文件窗口.ShowDialog() == true)
            {
                string 文件路径 = 打开文件窗口.FileName;
                计算器.打开或更换音频文件(文件路径);
                return true;
            }
            return false;
        }

        private void 播放按钮(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrEmpty(计算器.音频.当前播放文件路径)) { 打开音频文件(); 计算器.播放控制选项状态 = Visibility.Visible; 打开音频参考.Visibility = Visibility.Collapsed; }
            if (计算器.音频状态 == 节拍计算器._音频状态.播放) { 计算器.音频暂停(); return; }
            计算器.音频播放();
        }

        private void 加速按钮(object sender, RoutedEventArgs e)
        {
            计算器.音频.提升播放速度();
            日志.输出($"速度修改，当前为{计算器.音频.当前速度倍率}");
        }

        private void 减速按钮(object sender, RoutedEventArgs e)
        {
            计算器.音频.降低播放速度();
            日志.输出($"速度修改，当前为{计算器.音频.当前速度倍率}");
        }

        private void 按键(object sender, KeyEventArgs e)
        {
            if (节拍界面.Visibility == Visibility.Visible)
            {
                计算器.追加新的间隔时间点();
            }
            else
            {
                进入节拍计算器主界面();
            }

        }
    }

    public class BPM转换 : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            float data = (float)value;
            if (data > 999) { return "..."; }
            if (data < 0) { return "..."; }
            return Math.Round(data, MainWindow.计算器.BPM精度);
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new Exception();
        }
    }

    public class 偏移转换 : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            float data = (float)value;

            return $"{value}毫秒";
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new Exception();
        }
    }

    public class 自动切换状态转换 : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            bool data = (bool)value;
            if (data == true) { return 文本.停止自动寻拍按钮文本; }
            if (data == false) { return 文本.开始自动寻拍按钮文本; }
            return new Exception("不正确的寻拍按钮状态");
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new Exception();
        }
    }

}
